<?php

$team2 = $_REQUEST["team2"];
$team1 = $_REQUEST["team1"];
$matchPlayed = $_REQUEST["matchPlayed"];
$commitToDB = $_REQUEST["commitToDB"];

$team1Name=$team1;
$team2Name=$team2;


/*echo $team2;
 echo $team1;
 echo $matchPlayed;
 echo $commitToDB;
 
 echo "hi";
 exit();
 */


$pointsArray=array(1,2,4,25,50,10,50,100,5,10,10);
//1 run, fours, sixes, fifty, hundred, 1 wicket, 5wickets, 10ickets, catch, stumping, runout.

$playerScores=array();
//echo sizeof($playerScores);

/*******************************************DB**********************************************/
mysql_connect("d-12602", "root", "") or die(mysql_error());
mysql_select_db("cc_v1") or die(mysql_error());



/*
 $team2 = "Australia";
 $team1= "Bangladesh";
 $matchPlayed="match1";
 $commitToDB="false";
 */
$team1B0wlers=array();
$team2B0wlers=array();


$team1Batters=array();
$team2Batters=array();


$team1WK=array();
$team2WK=array();


$team1AR=array();
$team2AR=array();

/*******************************************TEAM 1-BOWLS first innings**********************************************/

$getteam1PlayersQuery='(SELECT * FROM player p where country = "'.$team1.'" and playerType="Bowler" limit 4) UNION
	(SELECT * FROM player p where country = "'.$team1.'" and playerType="Batsman" limit 5)UNION
	(SELECT * FROM player p where country = "'.$team1.'" and playerType="AllRounder" limit 1)UNION
	(SELECT * FROM player p where country = "'.$team1.'"and playerType="WicketKeeper" limit 1)';
$result1 = mysql_query($getteam1PlayersQuery) or die(mysql_error());  
$i=0;
$j=0;
while($team1 = mysql_fetch_assoc( $result1 )){
	if($team1["playerType"]=="Bowler"){
		$team1B0wlers[$i++]=$team1["id"];	
		$team1B0wlers[$i++]=$team1["fname"];
		$team1B0wlers[$i++]=$team1["lname"];
		$team1B0wlers[$i++]=$team1["playerType"];
	}
	else if($team1["playerType"]=="Batsman"){
		$team1Batters[$j++]=$team1["id"];
		$team1Batters[$j++]=$team1["fname"];
		$team1Batters[$j++]=$team1["lname"];
		$team1Batters[$j++]=$team1["playerType"];
	}else if($team1["playerType"]=="AllRounder"){
		$team1AR[0]=$team1["id"];
		$team1AR[1]=$team1["fname"];
		$team1AR[2]=$team1["lname"];
		$team1AR[3]=$team1["playerType"];		
	}else if($team1["playerType"]=="WicketKeeper"){
		$team1WK[0]=$team1["id"];	
		$team1WK[1]=$team1["fname"];
		$team1WK[2]=$team1["lname"];
		$team1WK[3]=$team1["playerType"];
		
	}
}
echo "<br>=================================================TEAM 1 : $team1Name=======================================================<br>";
$team1=array();
$team1=array_merge($team1,$team1Batters);
$team1=array_merge($team1,$team1AR);
$team1=array_merge($team1,$team1WK);
$team1=array_merge($team1,$team1B0wlers);
print_r($team1);
echo "<br>===============================================================================================================<br>";

/*******************************************TEAM 2 BATS first innings**********************************************/

$getteam2PlayersQuery='(SELECT * FROM player p where country = "'.$team2.'" and playerType="Bowler" limit 4) UNION
	(SELECT * FROM player p where country = "'.$team2.'" and playerType="Batsman" limit 5)UNION
	(SELECT * FROM player p where country = "'.$team2.'" and playerType="AllRounder" limit 1)UNION
	(SELECT * FROM player p where country = "'.$team2.'"and playerType="WicketKeeper" limit 1)';
$result2 = mysql_query($getteam2PlayersQuery) or die(mysql_error());  
$i=0;
$j=0;
while($team2 = mysql_fetch_assoc( $result2 )){
	if($team2["playerType"]=="Bowler"){
		$team2B0wlers[$i++]=$team2["id"];
		$team2B0wlers[$i++]=$team2["fname"];
		$team2B0wlers[$i++]=$team2["lname"];	
		$team2B0wlers[$i++]=$team2["playerType"];
	}
	else if($team2["playerType"]=="Batsman"){
		$team2Batters[$j++]=$team2["id"];
		$team2Batters[$j++]=$team2["fname"];
		$team2Batters[$j++]=$team2["lname"];
		$team2Batters[$j++]=$team2["playerType"];
	}else if($team2["playerType"]=="AllRounder"){
		$team2AR[0]=$team2["id"];	
		$team2AR[1]=$team2["fname"];
		$team2AR[2]=$team2["lname"];
		$team2AR[3]=$team2["playerType"];
	}else if($team2["playerType"]=="WicketKeeper"){
		$team2WK[0]=$team2["id"];	
		$team2WK[1]=$team2["fname"];
		$team2WK[2]=$team2["lname"];
		$team2WK[3]=$team2["playerType"];
	}
}
echo "<br>=================================================TEAM 2 : $team2Name=======================================================<br>";
$team2=array();
$team2=array_merge($team2,$team2Batters);
$team2=array_merge($team2,$team2AR);
$team2=array_merge($team2,$team2WK);
$team2=array_merge($team2,$team2B0wlers);
print_r($team2);
echo "<br>===============================================================================================================<br>";





echo "<br><b>============================================STARTING MATCH=====================================================</b><br>";


/**
 45,16,24,0,2,50,0,0,0,47,16,24,0,2,50,0,0,0
 45,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0
 45,16,24,0,2,50,0,0,0,45,16,24,0,2,50,0,0,0
 45,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0
 45,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0
 35,16,24,0,0,50,0,0,0,35,16,24,0,0,50,0,0,0
 25,16,24,0,4,50,0,0,0,25,16,24,0,4,50,0,0,0
 10,4,6,1,1,20,100,2,50,10,4,6,1,1,20,100,2,50
 10,4,6,4,1,20,100,2,80,10,4,6,4,1,20,100,2,80
 10,4,6,2,0,20,100,2,80,10,4,6,2,0,20,100,2,80
 10,4,6,3,0,20,100,2,50,10,4,6,3,0,20,100,2,50
 325,128,192,10,10,430,400,8,260,327,128,192,10,10,430,400,8,260
 total,4,6,w/c,catch,balls faced,balls bowled,maidens,runs_coneced,total,4,6,w/c,catch,balls faced,balls bowled,maidens,runs_coneced
 team 1 looses
 * 
 */
$match1=array(45,16,24,0,2,50,0,0,0,47,16,24,0,2,50,0,0,0,
	45,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0,
	45,16,24,0,2,50,0,0,0,45,16,24,0,2,50,0,0,0,
	45,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0,
	45,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0,
	35,16,24,0,0,50,0,0,0,35,16,24,0,0,50,0,0,0,
	25,16,24,0,4,50,0,0,0,25,16,24,0,4,50,0,0,0,
	10,4,6,1,1,20,100,2,50,10,4,6,1,1,20,100,2,50,
	10,4,6,4,1,20,100,2,80,10,4,6,4,1,20,100,2,80,
	10,4,6,2,0,20,100,2,80,10,4,6,2,0,20,100,2,80,
	10,4,6,3,0,20,100,2,50,10,4,6,3,0,20,100,2,50);
//print sizeof($match1);



/**
 50,16,24,0,2,50,0,0,0,47,16,24,0,2,50,0,0,0
 50,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0
 50,16,24,0,2,50,0,0,0,45,16,24,0,2,50,0,0,0
 50,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0
 50,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0
 35,16,24,0,0,50,0,0,0,35,16,24,0,0,50,0,0,0
 25,16,24,0,4,50,0,0,0,25,16,24,0,4,50,0,0,0
 10,4,6,1,1,20,100,2,50,10,4,6,1,1,20,100,2,50
 10,4,6,4,1,20,100,2,80,10,4,6,4,1,20,100,2,80
 10,4,6,2,0,20,100,2,80,10,4,6,2,0,20,100,2,80
 10,4,6,3,0,20,100,2,50,10,4,6,3,0,20,100,2,50
 350,128,192,10,10,430,400,8,260,327,128,192,10,10,430,400,8,260
 total,4,6,w/c,catch,balls faced,balls bowled,maidens,runs_coneced,total,4,6,w/c,catch,balls faced,balls bowled,maidens,runs_coneced
 
 team 2 looses
 * 
 */
$match2=array(50,16,24,0,2,50,0,0,0,47,16,24,0,2,50,0,0,0,
	50,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0,
	50,16,24,0,2,50,0,0,0,45,16,24,0,2,50,0,0,0,
	50,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0,
	50,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0,
	35,16,24,0,0,50,0,0,0,35,16,24,0,0,50,0,0,0,
	25,16,24,0,4,50,0,0,0,25,16,24,0,4,50,0,0,0,
	10,4,6,1,1,20,100,2,50,10,4,6,1,1,20,100,2,50,
	10,4,6,4,1,20,100,2,80,10,4,6,4,1,20,100,2,80,
	10,4,6,2,0,20,100,2,80,10,4,6,2,0,20,100,2,80,
	10,4,6,3,0,20,100,2,50,10,4,6,3,0,20,100,2,50);
//print sizeof($match2);



/**
 45,16,24,0,2,50,0,0,0,45,16,24,0,2,50,0,0,0
 45,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0
 45,16,24,0,2,50,0,0,0,45,16,24,0,2,50,0,0,0
 45,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0
 45,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0
 35,16,24,0,0,50,0,0,0,35,16,24,0,0,50,0,0,0
 25,16,24,0,4,50,0,0,0,25,16,24,0,4,50,0,0,0
 10,4,6,1,1,20,100,2,50,10,4,6,1,1,20,100,2,50
 10,4,6,4,1,20,100,2,80,10,4,6,4,1,20,100,2,80
 10,4,6,2,0,20,100,2,80,10,4,6,2,0,20,100,2,80
 10,4,6,3,0,20,100,2,50,10,4,6,3,0,20,100,2,50
 325,128,192,10,10,430,400,8,260,325,128,192,10,10,430,400,8,260
 total,4,6,w/c,catch,balls faced,balls bowled,maidens,runs_coneced,total,4,6,w/c,catch,balls faced,balls bowled,maidens,runs_coneced
 
 DRAW
 * 
 */
$match3=array(45,16,24,0,2,50,0,0,0,45,16,24,0,2,50,0,0,0,
	45,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0,
	45,16,24,0,2,50,0,0,0,45,16,24,0,2,50,0,0,0,
	45,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0,
	45,16,24,0,0,50,0,0,0,45,16,24,0,0,50,0,0,0,
	35,16,24,0,0,50,0,0,0,35,16,24,0,0,50,0,0,0,
	25,16,24,0,4,50,0,0,0,25,16,24,0,4,50,0,0,0,
	10,4,6,1,1,20,100,2,50,10,4,6,1,1,20,100,2,50,
	10,4,6,4,1,20,100,2,80,10,4,6,4,1,20,100,2,80,
	10,4,6,2,0,20,100,2,80,10,4,6,2,0,20,100,2,80,
	10,4,6,3,0,20,100,2,50,10,4,6,3,0,20,100,2,50);
//print sizeof($match3);

$i=0;
$j=18;//row 1 only for
$k=0;
if($matchPlayed=="match1"){
	echo "RUNNING MATCH 1<br>";
	$i=0;
	$j=18;//row 1 only for
	$k=0;
	for($k=0;$k<sizeof($team1);$k+=4){
		for($i=0;$i<$j;$i+=18){
			$performanceQuery="Insert into performance (playerId, entityType, runs_scored, balls_faced," .
				" isout, sixes, fours, balls_bowled, maidens, wickets, runs_conceded, wides, noballs, catches," .
				"stumpings, byes, runouts, numIndex) values (".$team1[$k].", 'entityType', ".$match1[$i].", ".$match1[$i+5]."" .
				", 1, ".($match1[$i+2]/6).", ".($match1[$i+1]/4).", ".$match1[$i+6].", ".$match1[$i+7].", "
				.$match1[$i+3].", ".$match1[$i+8].", ".mt_rand(0,5).", ".mt_rand(0,5).", ".$match1[$i+4].", "
				.mt_rand(0,3).", ".mt_rand(0,5).", ".mt_rand(0,2).", ".mt_rand(0,500)." )" ;
			$statQuery="update stat set matches=matches+1, innings=innings+1, runs = runs + ".$match1[$i] .", sixes= sixes + ".($match1[$i+2]/6).
				", fours= fours + ".($match1[$i+1]/4).", wickets= wickets + ".$match1[$i+3].
				", catches= catches + ".$match1[$i+4]. " where stattype='ODI' and playerId=".$team1[$k];
			$score = $match1[$i]*$pointsArray[0] + ($match1[$i+1]/4)*$pointsArray[1] + ($match1[$i+2]/6)*$pointsArray[2]
																													  + $match1[$i+3]*$pointsArray[5]+ $match1[$i+4]*$pointsArray[8];
			
			if($match1[$i]>=50&&$match1[$i]<100){
				$score+=$pointsArray[3];
			}else if($match1[$i]>=100){
				$score+=$pointsArray[4];
			}
			if($match1[$i+3]>=5&&$match1[$i+3]<10){
				$score+=$pointsArray[6];
			}else if($match1[$i+3]>=10){
				$score+=$pointsArray[7];
			}
			
			
			
		}
		echo $score."<br>";	
		echo $performanceQuery."<br>";
		echo $statQuery."<br>";
		$playerScores[sizeof($playerScores)]=$team1[$k];
		$playerScores[sizeof($playerScores)]=$score;
		if($commitToDB=="true"){
			mysql_query($performanceQuery);
			mysql_query($statQuery);
		}
		
		$j+=18;
	}
	
	$i=0;
	$j=18;//row 1 only for
	$k=0;
	for($k=0;$k<sizeof($team2);$k+=4){
		for($i=0;$i<$j;$i+=18){
			$performanceQuery="Insert into performance (playerId, entityType, runs_scored, balls_faced," .
				" isout, sixes, fours, balls_bowled, maidens, wickets, runs_conceded, wides, noballs, catches," .
				"stumpings, byes, runouts, numIndex) values (".$team2[$k].", 'entityType', ".$match1[$i+9].", ".$match1[$i+14]."" .
				", 1, ".($match1[$i+11]/6).", ".($match1[$i+10]/4).", ".$match1[$i+15].", ".$match1[$i+16].", "
				.$match1[$i+12].", ".$match1[$i+17].", ".mt_rand(0,5).", ".mt_rand(0,5).", ".$match1[$i+13].", "
				.mt_rand(0,3).", ".mt_rand(0,5).", ".mt_rand(0,2).", ".mt_rand(0,500)." )" ;
			$statQuery="update stat set matches=matches+1, innings=innings+1, runs = runs + ".$match1[$i+9] .", sixes= sixes + ".($match1[$i+11]/6).
				", fours= fours + ".($match1[$i+10]/4).", wickets= wickets + ".$match1[$i+12].
				", catches= catches + ".$match1[$i+13]. " where stattype='ODI' and playerId=".$team2[$k];
			$score = $match1[$i+9]*$pointsArray[0] + ($match1[$i+10]/4)*$pointsArray[1] + ($match1[$i+11]/4)*$pointsArray[2]
																														  + $match1[$i+12]*$pointsArray[5]+ $match1[$i+13]*$pointsArray[8];
			
			if($match1[$i+9]>=50&&$match1[$i+9]<100){
				$score+=$pointsArray[3];
			}else if($match1[$i+9]>=100){
				$score+=$pointsArray[4];
			}
			if($match1[$i+12]>=5&&$match1[$i+12]<10){
				$score+=$pointsArray[6];
			}else if($match1[$i+12]>=10){
				$score+=$pointsArray[7];
			}						
			
		}
		echo $performanceQuery."<br>";
		echo $statQuery."<br>";
		echo $score."<br>";	
		$playerScores[sizeof($playerScores)]=$team2[$k];
		$playerScores[sizeof($playerScores)]=$score;
		if($commitToDB=="true"){
			mysql_query($performanceQuery);
			mysql_query($statQuery);	
		}
		$j+=18;
	}
}
elseif($matchPlayed=="match2"){
	echo "RUNNING MATCH 2<br>";
	$i=0;
	$j=18;//row 1 only for
	$k=0;
	for($k=0;$k<sizeof($team1);$k+=4){
		for($i=0;$i<$j;$i+=18){
			$performanceQuery="Insert into performance (playerId, entityType, runs_scored, balls_faced," .
				" isout, sixes, fours, balls_bowled, maidens, wickets, runs_conceded, wides, noballs, catches," .
				"stumpings, byes, runouts, numIndex) values (".$team1[$k].", 'entityType', ".$match2[$i].", ".$match2[$i+5]."" .
				", 1, ".($match2[$i+2]/6).", ".($match2[$i+1]/4).", ".$match2[$i+6].", ".$match2[$i+7].", "
				.$match2[$i+3].", ".$match2[$i+8].", ".mt_rand(0,5).", ".mt_rand(0,5).", ".$match2[$i+4].", "
				.mt_rand(0,3).", ".mt_rand(0,5).", ".mt_rand(0,2).", ".mt_rand(0,500)." )" ;
			$statQuery="update stat set matches=matches+1, innings=innings+1, runs = runs + ".$match2[$i] .", sixes= sixes + ".($match2[$i+2]/6).
				", fours= fours + ".($match2[$i+1]/4).", wickets= wickets + ".$match2[$i+3].
				", catches= catches + ".$match2[$i+4]. " where stattype='ODI' and playerId=".$team1[$k];
			$score = $match2[$i]*$pointsArray[0] + ($match2[$i+1]/4)*$pointsArray[1] + ($match2[$i+2]/6)*$pointsArray[2]
																													  + $match2[$i+3]*$pointsArray[5]+ $match2[$i+4]*$pointsArray[8];
			
			if($match2[$i]>=50&&$match2[$i]<100){
				$score+=$pointsArray[3];
			}else if($match2[$i]>=100){
				$score+=$pointsArray[4];
			}
			if($match2[$i+3]>=5&&$match2[$i+3]<10){
				$score+=$pointsArray[6];
			}else if($match2[$i+3]>=10){
				$score+=$pointsArray[7];
			}
			
			
			
		}
		echo $score."<br>";	
		echo $performanceQuery."<br>";
		echo $statQuery."<br>";
		$playerScores[sizeof($playerScores)]=$team1[$k];
		$playerScores[sizeof($playerScores)]=$score;
		if($commitToDB=="true"){
			mysql_query($performanceQuery);
			mysql_query($statQuery);
		}
		
		$j+=18;
	}
	
	$i=0;
	$j=18;//row 1 only for
	$k=0;
	for($k=0;$k<sizeof($team2);$k+=4){
		for($i=0;$i<$j;$i+=18){
			$performanceQuery="Insert into performance (playerId, entityType, runs_scored, balls_faced," .
				" isout, sixes, fours, balls_bowled, maidens, wickets, runs_conceded, wides, noballs, catches," .
				"stumpings, byes, runouts, numIndex) values (".$team2[$k].", 'entityType', ".$match2[$i+9].", ".$match2[$i+14]."" .
				", 1, ".($match2[$i+11]/6).", ".($match2[$i+10]/4).", ".$match2[$i+15].", ".$match2[$i+16].", "
				.$match2[$i+12].", ".$match2[$i+17].", ".mt_rand(0,5).", ".mt_rand(0,5).", ".$match2[$i+13].", "
				.mt_rand(0,3).", ".mt_rand(0,5).", ".mt_rand(0,2).", ".mt_rand(0,500)." )" ;
			$statQuery="update stat set matches=matches+1, innings=innings+1, runs = runs + ".$match2[$i+9] .", sixes= sixes + ".($match2[$i+11]/6).
				", fours= fours + ".($match2[$i+10]/4).", wickets= wickets + ".$match2[$i+12].
				", catches= catches + ".$match2[$i+13]. " where stattype='ODI' and playerId=".$team2[$k];
			$score = $match2[$i+9]*$pointsArray[0] + ($match2[$i+10]/4)*$pointsArray[1] + ($match2[$i+11]/4)*$pointsArray[2]
																														  + $match2[$i+12]*$pointsArray[5]+ $match2[$i+13]*$pointsArray[8];
			
			if($match2[$i+9]>=50&&$match2[$i+9]<100){
				$score+=$pointsArray[3];
			}else if($match2[$i+9]>=100){
				$score+=$pointsArray[4];
			}
			if($match2[$i+12]>=5&&$match2[$i+12]<10){
				$score+=$pointsArray[6];
			}else if($match2[$i+12]>=10){
				$score+=$pointsArray[7];
			}						
			
		}
		echo $performanceQuery."<br>";
		echo $statQuery."<br>";
		echo $score."<br>";	
		$playerScores[sizeof($playerScores)]=$team2[$k];
		$playerScores[sizeof($playerScores)]=$score;
		if($commitToDB=="true"){
			mysql_query($performanceQuery);
			mysql_query($statQuery);	
		}
		$j+=18;
	}
}
else {
	echo "RUNNING MATCH 3<br>";
	$i=0;
	$j=18;//row 1 only for
	$k=0;
	for($k=0;$k<sizeof($team1);$k+=4){
		for($i=0;$i<$j;$i+=18){
			$performanceQuery="Insert into performance (playerId, entityType, runs_scored, balls_faced," .
				" isout, sixes, fours, balls_bowled, maidens, wickets, runs_conceded, wides, noballs, catches," .
				"stumpings, byes, runouts, numIndex) values (".$team1[$k].", 'entityType', ".$match3[$i].", ".$match3[$i+5]."" .
				", 1, ".($match3[$i+2]/6).", ".($match3[$i+1]/4).", ".$match3[$i+6].", ".$match3[$i+7].", "
				.$match3[$i+3].", ".$match3[$i+8].", ".mt_rand(0,5).", ".mt_rand(0,5).", ".$match3[$i+4].", "
				.mt_rand(0,3).", ".mt_rand(0,5).", ".mt_rand(0,2).", ".mt_rand(0,500)." )" ;
			$statQuery="update stat set matches=matches+1, innings=innings+1, runs = runs + ".$match3[$i] .", sixes= sixes + ".($match3[$i+2]/6).
				", fours= fours + ".($match3[$i+1]/4).", wickets= wickets + ".$match3[$i+3].
				", catches= catches + ".$match3[$i+4]. " where stattype='ODI' and playerId=".$team1[$k];
			$score = $match3[$i]*$pointsArray[0] + ($match3[$i+1]/4)*$pointsArray[1] + ($match3[$i+2]/6)*$pointsArray[2]
																													  + $match3[$i+3]*$pointsArray[5]+ $match3[$i+4]*$pointsArray[8];
			
			if($match3[$i]>=50&&$match3[$i]<100){
				$score+=$pointsArray[3];
			}else if($match3[$i]>=100){
				$score+=$pointsArray[4];
			}
			if($match3[$i+3]>=5&&$match3[$i+3]<10){
				$score+=$pointsArray[6];
			}else if($match3[$i+3]>=10){
				$score+=$pointsArray[7];
			}
			
			
			
		}
		echo $score."<br>";	
		echo $performanceQuery."<br>";
		echo $statQuery."<br>";
		$playerScores[sizeof($playerScores)]=$team1[$k];
		$playerScores[sizeof($playerScores)]=$score;
		if($commitToDB=="true"){
			mysql_query($performanceQuery);
			mysql_query($statQuery);
		}
		
		$j+=18;
	}
	
	$i=0;
	$j=18;//row 1 only for
	$k=0;
	for($k=0;$k<sizeof($team2);$k+=4){
		for($i=0;$i<$j;$i+=18){
			$performanceQuery="Insert into performance (playerId, entityType, runs_scored, balls_faced," .
				" isout, sixes, fours, balls_bowled, maidens, wickets, runs_conceded, wides, noballs, catches," .
				"stumpings, byes, runouts, numIndex) values (".$team2[$k].", 'entityType', ".$match3[$i+9].", ".$match3[$i+14]."" .
				", 1, ".($match3[$i+11]/6).", ".($match3[$i+10]/4).", ".$match3[$i+15].", ".$match3[$i+16].", "
				.$match3[$i+12].", ".$match3[$i+17].", ".mt_rand(0,5).", ".mt_rand(0,5).", ".$match3[$i+13].", "
				.mt_rand(0,3).", ".mt_rand(0,5).", ".mt_rand(0,2).", ".mt_rand(0,500)." )" ;
			$statQuery="update stat set matches=matches+1, innings=innings+1, runs = runs + ".$match3[$i+9] .", sixes= sixes + ".($match3[$i+11]/6).
				", fours= fours + ".($match3[$i+10]/4).", wickets= wickets + ".$match3[$i+12].
				", catches= catches + ".$match3[$i+13]. " where stattype='ODI' and playerId=".$team2[$k];
			", catches= catches + ".$match3[$i+4]. " where stattype='ODI' and playerId=".$team2[$k];
			$score = $match3[$i+9]*$pointsArray[0] + ($match3[$i+10]/4)*$pointsArray[1] + ($match3[$i+11]/4)*$pointsArray[2]
																														  + $match3[$i+12]*$pointsArray[5]+ $match3[$i+13]*$pointsArray[8];
			
			if($match3[$i+9]>=50&&$match3[$i+9]<100){
				$score+=$pointsArray[3];
			}else if($match3[$i+9]>=100){
				$score+=$pointsArray[4];
			}
			if($match3[$i+12]>=5&&$match3[$i+12]<10){
				$score+=$pointsArray[6];
			}else if($match3[$i+12]>=10){
				$score+=$pointsArray[7];
			}						
			
		}
		echo $performanceQuery."<br>";
		echo $statQuery."<br>";
		echo $score."<br>";	
		$playerScores[sizeof($playerScores)]=$team2[$k];
		$playerScores[sizeof($playerScores)]=$score;
		if($commitToDB=="true"){
			mysql_query($performanceQuery);
			mysql_query($statQuery);	
		}
		$j+=18;
	}
}

print_r($playerScores);

updateUserScores($commitToDB,$playerScores);

function updateUserScores($commitToDB,$playerScores){
	$strQuery = "Select id,currentTeamId,score from cc_V1.users";
	$result = mysql_query($strQuery);
	while($row = mysql_fetch_assoc($result)){
		//echo "<br>ID: ".$row["id"];
		//echo "<br>Team: ".$row["currentTeamId"];
		$points=$row["score"];
		$strQuery2 = "Select * from cc_V1.team where id = ".$row["currentTeamId"].";";
		$result2 = mysql_query($strQuery2);
		if($result2 && mysql_num_rows($result2)>0){
			while($row2 = mysql_fetch_assoc($result2)){
				foreach ( $row2 as $key => $value ) {
					if(strpos($key, 'Id')>0 && strcasecmp($key,"userId")!=0)
						$playerId=$value;
					for ($index = 0, $max_count = sizeof( $playerScores ); $index < $max_count; $index+=2) {
						$array_element = $playerScores[ $index ];
						if ($value ==$playerScores[ $index ])
							$points = $points + $playerScores[ $index + 1 ];
					}
				}
			}
		}
		$strQuery4 = "<br>"."Update users set score=$points where id=".$row["id"];
		//echo $strQuery4;
		if($commitToDB=="true"){
			mysql_query($strQuery4);
		}
	}
}

?>

